# -*- coding: UTF-8 -*-
import urllib2
import sys
import mongodbClient
reload(sys)
sys.setdefaultencoding('utf-8')
from bs4 import BeautifulSoup
def jdall(url):
    page = urllib2.urlopen(url)
    html_doc = page.read()
    soup = BeautifulSoup(html_doc.decode('gb2312','ignore'))
    for i in soup.find_all('div', id="quotesearch"):
        li=i.find_all('li')
        for lia in  li:
            atag=lia.find_all('a')
            for atext in atag:
                bb=atext.text
                bc = bb.encode()
                u1,u2=bc.split('(',1)
                print(u1)
                u3=u2.replace(")","")
                print(u3)
                try:
                    mongodbClient.insertMG(u3,"codename",u1,"名字")
                except Exception as e:
                    mongodbClient.insertAndUpdate(u3,"codename",u1,"名字")
                    print(e)
jdall("http://quote.eastmoney.com/stocklist.html#sh")